package org.apache.dubbo.demo.consumer.ndcg;

import org.junit.Test;

/**
 * 10，7，8，9，4，5，3，9，2，7，6，6，2
 */
public class NDCG {

  private static int[] arr = {10, 7, 8, 9, 4, 5, 3, 9, 2, 7, 6, 6, 2};
  private static int[] sortedArr = {10, 9, 9, 8, 7, 7, 6, 6, 5, 4, 3, 2, 2};

  @Test
  public void compute() {
    System.out.println((1 / Math.sqrt(2)));
  }

  @Test
  public void testNdcg() {
    double dcg = 0, idcg = 0;
    for (int i = 1; i <= arr.length; i++) {
      dcg += item(arr[i - 1], i);
      idcg += item(sortedArr[i - 1], i);
    }

    System.out.println(dcg / idcg);
  }

  private double item(int score, int i) {
    return molecule(score) / denominator(i);
  }

  private double denominator(int i) {
    return Math.log(i + 1) / Math.log(2);
  }

  private double molecule(int score) {
    return Math.pow(2, score) - 1;
  }
}
